/*
*  Copyright 2019-2023 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.poemmanager.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import me.zhengjie.poemmanager.domain.ObjectLike;
import me.zhengjie.poemmanager.domain.vo.ObjectLikeQueryCriteria;
import me.zhengjie.poemmanager.mapper.ObjectLikeMapper;
import me.zhengjie.poemmanager.service.ObjectLikeService;
import me.zhengjie.utils.FileUtil;
import me.zhengjie.utils.PageResult;
import me.zhengjie.utils.PageUtil;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
* @description 服务实现
* @author hwl
* @date 2024-12-11
**/
@Service
@RequiredArgsConstructor
public class ObjectLikeServiceImpl extends ServiceImpl<ObjectLikeMapper, ObjectLike> implements ObjectLikeService {

    private final ObjectLikeMapper objectLikeMapper;

    @Override
    public PageResult<ObjectLike> queryAll(ObjectLikeQueryCriteria criteria, Page<Object> page){
        return PageUtil.toPage(objectLikeMapper.findAll(criteria, page));
    }

    @Override
    public List<ObjectLike> queryAll(ObjectLikeQueryCriteria criteria){
        return objectLikeMapper.findAll(criteria);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ObjectLike create(ObjectLike resources) {
        switch (resources.getObjectType()) {
            case 4:
                objectLikeMapper.addCommentLikeCount(resources.getObjectId());
                break;
            case 5:
                objectLikeMapper.addDiscussionLikeCount(resources.getObjectId());
                break;
            default:
                throw new IllegalArgumentException("无效的点赞类型: " + resources.getObjectType());
        }
        try {
            save(resources);
        } catch (DuplicateKeyException e) { // 直接捕获唯一约束异常
            throw new RuntimeException("插入失败，不可重复点赞");
        }
        return resources;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(ObjectLike resources) {
        ObjectLike objectLike = getById(resources.getLikeId());
        objectLike.copy(resources);
        saveOrUpdate(objectLike);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteAll(List<Integer> ids) {
        for(Integer id : ids) {
            ObjectLike resources = objectLikeMapper.selectById(id);
            switch (resources.getObjectType()) {
                case 4:
                    objectLikeMapper.subCommentLikeCount(resources.getObjectId());
                    break;
                case 5:
                    objectLikeMapper.subDiscussionLikeCount(resources.getObjectId());
                    break;
                default:
                    throw new IllegalArgumentException("无效的点赞类型: " + resources.getObjectType());
            }
        }
        removeBatchByIds(ids);
    }

    @Override
    public void download(List<ObjectLike> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (ObjectLike objectLike : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("点赞对象", objectLike.getObjectId());
            map.put("0帖子，1用户诗歌，2诗人诗歌，3朗诵", objectLike.getObjectType());
            map.put("用户名", objectLike.getUserName());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }

    @Override
    public ObjectLike isLike(ObjectLike like) {
        return objectLikeMapper.isLike(like);
    }
}